{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.4. Creating an IPython extension with custom magic commands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from IPython.core.magic import (register_line_magic,\n",
    "                                register_cell_magic)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "@register_line_magic\n",
    "def hello(line):\n",
    "    if line == 'french':\n",
    "        print(\"Salut tout le monde!\")\n",
    "    else:\n",
    "        print(\"Hello world!\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "%hello"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "%hello french"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from io import StringIO\n",
    "\n",
    "@register_cell_magic\n",
    "def csv(line, cell):\n",
    "    # We create a string buffer containing the\n",
    "    # contents of the cell.\n",
    "    sio = StringIO(cell)\n",
    "    # We use Pandas' read_csv function to parse\n",
    "    # the CSV string.\n",
    "    return pd.read_csv(sio)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "%%csv\n",
    "col1,col2,col3\n",
    "0,1,2\n",
    "3,4,5\n",
    "7,8,9"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df = _\n",
    "df.describe()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "%%writefile csvmagic.py\n",
    "import pandas as pd\n",
    "from io import StringIO\n",
    "\n",
    "def csv(line, cell):\n",
    "    sio = StringIO(cell)\n",
    "    return pd.read_csv(sio)\n",
    "\n",
    "def load_ipython_extension(ipython):\n",
    "    \"\"\"This function is called when the extension is\n",
    "    loaded. It accepts an IPython InteractiveShell\n",
    "    instance. We can register the magic with the\n",
    "    `register_magic_function` method of the shell\n",
    "    instance.\"\"\"\n",
    "    ipython.register_magic_function(csv, 'cell')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "%load_ext csvmagic"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "%%csv\n",
    "col1, col2, col3\n",
    "0,1,2\n",
    "3,4,5\n",
    "7,8,9"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
